/**
 * (c)Copyright 2008 SinoCredit Co,Ltd. All Rights Reserved.
 * GftUserAction.java, 1:21:24 PM
 * @author yipeng
 * @since Jan 9, 2008
 * @version 1.0
 * change list:
 * yipeng   (创建)
 */

package com.sys.web.action;

import java.awt.image.RenderedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;

import com.sys.base.UserBean;
import com.sys.base.action.BaseGftAction;
import com.sys.dao.model.GftRight;
import com.sys.dao.model.GftRole;
import com.sys.dao.model.GftUserInfo;
import com.sys.plat.bean.PageBean;
import com.sys.plat.util.HttpUtil;
import com.sys.plat.web.core.ActionContext;
import com.sys.service.GftUserService;
import com.sys.system.Constants;
import com.sys.web.form.GftUserForm;
import com.sys.web.util.DrawRandomNumber;
import com.sys.web.util.SystemUtil;

public class GftUserAction extends BaseGftAction {

    /**
     * gft用户登录校验
     *
     * @return
     */
    public String login() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserInfo user = form.getU();
        HttpSession session = ActionContext.getRequest().getSession();
        // 验证码
        String valCode = form.getValcode();
        String sessValCode = (String) session.getAttribute(Constants.RANDOMVALUE_KEY);
        if (null == sessValCode || null == valCode
                || 0 == valCode.length() || !(sessValCode.equals(valCode))) {
            addError("VALCODE_FAILED");
            HttpUtil.reqSetAttr("u", user);
            return "login";
        }
        session.removeAttribute(Constants.RANDOMVALUE_KEY);
        // 校验用户密码
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        UserBean userBean = service.login(user.getUserName(), user.getPassWord());
        if (null == userBean) {
            addError("LOGIN_FAILED");
            HttpUtil.reqSetAttr("u", user);
            return "login";
        }
        session.setAttribute(Constants.USERBEAN, userBean);
        return "normaluser";
    }

    public String valCode() {
        ActionContext.getResponse().setHeader("Cache-Control", "no-cache"); // Forces caches to
        ActionContext.getResponse().setHeader("Cache-Control", "no-store"); // Directs caches
        ActionContext.getResponse().setDateHeader("Expires", 0); // Causes the proxy cache to see
        ActionContext.getResponse().setHeader("Pragma", "no-cache"); // HTTP 1.0 backward
        try {
            ImageIO.write((RenderedImage) DrawRandomNumber.getInstance()
                    .drawRandNumber(4, 10, ActionContext.getRequest()), "JPEG",
                    ActionContext.getResponse().getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String logout() {
        ActionContext.getRequest().getSession().invalidate();
        return "logout";
    }

    public String findUser() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        PageBean pageBean = super.createPageBean(form);
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.list(pageBean, "GftUserInfo");
        pageBean.setToWeb();
        HttpUtil.saveSearchParemeter();
        HttpUtil.reqSetAttr("fmap", form.getFilter());
        return "listuser";
    }

    /**
     * 跳转到添劄1�7/编辑gft用户页面
     *
     * @return
     */
    public String editUser() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserInfo user = form.getU();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        if (null != user.getId() && !"".equals(user.getId())) {
            user = service.editUser(user.getId());
            HttpUtil.reqSetAttr("roles", service.getRolesByUserId(user.getId()));
        }
        HttpUtil.reqSetAttr("u", user);
        HttpUtil.saveSearchParemeter();
        return "edituser";
    }

    /**
     * 添加/更新gft用户
     *
     * @return
     */
    public String updateUser() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        GftUserInfo user = form.getU();
        if (service.checkObj("GftUserInfo", "userName", user.getUserName())) {
            addError("USER_EXISTS_ALREADY");
            return this.editUser();
        }
        String id = user.getId();
        service.addOrUpdateUser(user);
        if (null == id || "".equals(id))
            addMsg("ADD_USER_OK");
        else
            addMsg("EDIT_USER_OK");
        return this.editUser();
    }

    public String deleteUser() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.deleteUser(form.getIds());
        return this.findUser();
    }

    /**
     * 切换用户状�1�7�1�7
     *
     * @return
     */
    public String switchUserStatus() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserInfo user = form.getU();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.switchStatus(user, GftUserInfo.class);
        return this.findUser();
    }

    /**
     * 弹出配置用户角色页面
     *
     * @return
     */
    public String selectRoles() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserInfo user = form.getU();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        if (null != user.getId() && !"".equals(user.getId())) {
            user = service.editUser(user.getId());
            HttpUtil.reqSetAttr("u", user);
            HttpUtil.reqSetAttr("roleIds", service.getRoleIdsByUserId(user.getId()));
        }
        PageBean pageBean = createPageBean(form);
        service.list(pageBean, "GftRole");
        pageBean.setToWeb();
        HttpUtil.saveSearchParemeter();
        return "configrole";
    }

    /**
     * 更新用户-角色衄1�7
     *
     * @return
     */
    public String configRoles() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserInfo user = form.getU();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        String[] roleIdArray = form.getConfigId();
        service.updateUserRole(user.getId(), roleIdArray);
        return "close";
    }

    public String delUserRole() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.delUserRole(form.getConfigId());
        return this.editUser();
    }

    public String findRole() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        PageBean pageBean = super.createPageBean(form);
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.list(pageBean, "GftRole");
        pageBean.setToWeb();
        HttpUtil.saveSearchParemeter();
        HttpUtil.reqSetAttr("fmap", form.getFilter());
        return "listrole";
    }

    public String editRole() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftRole role = form.getR();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        if (null != role.getId() && !"".equals(role.getId())) {
            role = service.editRole(role.getId());
            HttpUtil.reqSetAttr("r", role);
            HttpUtil.reqSetAttr("rights", service.getRightsByRoleId(role.getId()));
        }
        HttpUtil.saveSearchParemeter();
        return "editrole";
    }

    public String updateRole() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        GftRole role = form.getR();
        if (service.checkObj("GftRole", "roleName", role.getRoleName())) {
            addError("ROLE_EXISTS_ALREADY");
            return this.editRole();
        }
        String id = role.getId();
        service.addOrUpdate(role);
        if (null == id || "".equals(id))
            addMsg("ADD_ROLE_OK");
        else
            addMsg("EDIT_ROLE_OK");
        return this.editRole();
    }

    public String deleteRole() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.deleteRole(form.getIds());
        return this.findRole();
    }

    public String switchRoleStatus() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftRole role = form.getR();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.switchStatus(role, GftRole.class);
        return this.findRole();
    }

    /**
     * 弹出配置角色权限页面
     *
     * @return
     */
    public String selectRights() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftRole role = form.getR();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        if (null != role.getId() && !"".equals(role.getId())) {
            role = service.editRole(role.getId());
            HttpUtil.reqSetAttr("r", role);
            HttpUtil.reqSetAttr("rightIds", service.getRightIdsByRoleId(role.getId()));
        }

        PageBean pageBean = createPageBean(form);
        service.list(pageBean, "GftRight");
        pageBean.setToWeb();
        HttpUtil.saveSearchParemeter();
        HttpUtil.reqSetAttr("fmap", form.getFilter());
        return "configright";
    }

    /**
     * 更新角色-权限表
     *
     * @return 关闭弹出页
     */
    public String configRights() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftRole role = form.getR();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        String[] rightIdArray = form.getConfigId();
        service.updateRoleRight(role.getId(), rightIdArray);
        return "close";
    }

    public String delRoleRight() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.delRoleRight(form.getConfigId());
        return this.editRole();
    }

    public String findRight() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        PageBean pageBean = createPageBean(form);
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.list(pageBean, "GftRight");
        pageBean.setToWeb();
        HttpUtil.saveSearchParemeter();
        HttpUtil.reqSetAttr("fmap", form.getFilter());
        return "listright";
    }

    public String editRight() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftRight right = form.getT();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        if (null != right.getId() && !"".equals(right.getId())) {
            right = service.editRight(right.getId());
            HttpUtil.reqSetAttr("t", right);
            HttpUtil.saveSearchParemeter();
        }
        return "editright";
    }

    public String updateRight() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftRight right = form.getT();
        String id = right.getId();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.addOrUpdate(right);
        if (null == id || "".equals(id))
            addMsg("ADD_RIGHT_OK");
        else
            addMsg("EDIT_RIGHT_OK");
        return this.editRight();
    }

    public String deleteRight() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.deleteRight(form.getIds());
        return this.findRight();
    }

    /**
     * 修改用户密码
     * @return 密码修改页面
     */
    public String modifyUser() {
		GftUserInfo u = SystemUtil.getLoginUser().getGftUserInfo();
		HttpUtil.reqSetAttr("u", u);
		HttpUtil.saveSearchParemeter();

		return "modifyuser";
	}

    /**
     * 更新用户密码
     *
     * @return 密码修改页面
     */
    public String updateModify() {
		GftUserForm form = (GftUserForm) ActionContext.getForm();
        String uPasswd = form.getU().getPassWord(); // 新密砄1�7
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        GftUserInfo u = service.editUser(form.getU().getId());
        String passWord = form.getPassWord(); // 再次输入新密砄1�7
		String oldPassWord = u.getPassWord(); // 数据库保存的密码
        // 比较数据库保存的密码和输入的旧密砄1�7
        if (!oldPassWord.equals(form.getModPassWord())) {
			HttpUtil.reqSetAttr("u", u);
			addError("USER_PASSWD_WRONG");
			return "modifyuser";
		}
        // 两次输入的新密码均为空则密码不变
        if ( (null == passWord || "".equals(passWord)) && (null == uPasswd
				|| "".equals(uPasswd)) ) {
			return "close";
		} else if ( ( (null == passWord && null != uPasswd)
				|| (null != passWord && null == uPasswd) )
				|| !passWord.equals(uPasswd) ) { // 两次密码输入不一臄1�7
			HttpUtil.reqSetAttr("u", u);
			addError("USER_PASSWD_WRONG");
			return "modifyuser";
		}
        u.setPassWord(uPasswd);
        service.addOrUpdate(u); // 修改密码
//		addMsg("EDIT_USER_OK");
//		HttpUtil.reqSetAttr("u", u);

		return "close";
	}

    public String switchRightStatus() {
        GftUserForm form = (GftUserForm) ActionContext.getForm();
        GftRight right = form.getT();
        GftUserService service = (GftUserService) getService("gftUserServiceImpl");
        service.switchStatus(right, GftRight.class);
        return this.findRight();
    }
}